﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Liang.Device.Camera.Dll
{
    /// <summary>
    /// 艾普视达摄像头DLL
    /// </summary>
    static class AipStarDll
    {
        /// <summary>
        /// 初始化摄像头SDK
        /// </summary>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_InitSDK();

        /// <summary>
        /// 注销SDK
        /// </summary>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern void KsCamera_AipStar_CleanupSDK();

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="nFlag">初始化摄像头类型</param>
        /// <returns>设备控制句柄</returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern IntPtr KsCamera_AipStar_Login(uint nFlag);

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="hCamera">设备句柄</param>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern void KsCamera_AipStar_Logout(IntPtr hCamera);

        /// <summary>
        /// 实时播放
        /// </summary>
        /// <param name="hCamera">设备句柄</param>
        /// <param name="hPlayWnd">播放窗口句柄</param>
        /// <param name="szIPAddr">摄像头IP地址</param>
        /// <param name="nPort">端口号</param>
        /// <param name="szUser">用户名</param>
        /// <param name="szPassword">密码</param>
        /// <param name="nChannel">通道号</param>
        /// <param name="nStream">码流（0：主码流 1：次码流）</param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_Play(IntPtr hCamera, IntPtr hPlayWnd, string szIPAddr, int nPort, string szUser, string szPassword, UInt16 nChannel, UInt16 nStream);

        /// <summary>
        /// 停止播放
        /// </summary>
        /// <param name="hCamera">设备句柄</param>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern void KsCamera_AipStar_StopPlay(IntPtr hCamera);

        /// <summary>
        /// 连接（实时抓拍前必须先连接）
        /// </summary>
        /// <param name="hCamera"></param>
        /// <param name="szIPAddr"></param>
        /// <param name="nPort"></param>
        /// <param name="szUser"></param>
        /// <param name="szPassword"></param>
        /// <param name="bSync"></param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_Connect(IntPtr hCamera, string szIPAddr, int nPort, string szUser, string szPassword, Boolean bSync);

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="hCamera"></param>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern void KsCamera_AipStar_DisConnect(IntPtr hCamera);

        // BOOL WINAPI KsCamera_AipStar_RealCapture(HANDLE hCamera, BYTE nChannel, BYTE nStream, LPCTSTR szFileName);
        /// <summary>
        /// 实时抓拍
        /// </summary>
        /// <param name="hCamera"></param>
        /// <param name="nChannel"></param>
        /// <param name="nStream">码流必须支持JPEG格式</param>
        /// <param name="szFileName">抓拍图片文件名</param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_RealCapture(IntPtr hCamera, byte nChannel, byte nStream, string szFileName);

        // BOOL WINAPI KsCamera_AipStar_Capture(HANDLE hCamera, LPCTSTR szFileName)
        /// <summary>
        /// 预览抓拍
        /// <para>必须先打开视频预览，否则无法抓拍图片</para>
        /// </summary>
        /// <param name="hCamera">摄像头句柄</param>
        /// <param name="szFileName">文件名</param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_Capture(IntPtr hCamera, string szFileName);

        // BOOL WINAPI KsCamera_AipStar_CameraVideoCfg(HANDLE hCamera, BYTE nChannel, BYTE nStream, BYTE nResolution, BYTE nColorTransMode, BYTE nShutterSpeed)
        /// <summary>设置摄像头参数</summary>
        /// <param name="hCamera">摄像头句柄</param>
        /// <param name="nChannel">通道号</param>
        /// <param name="nStream">码流</param>
        /// <param name="nResolution">编分辨率</param>
        /// <param name="nColorTransMode">>黑白或彩色</param>
        /// <param name="nShutterSpeed">快门</param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_CameraVideoCfg(IntPtr hCamera, byte nChannel, byte nStream, byte nResolution, byte nColorTransMode, byte nShutterSpeed);


        /// <summary>
        /// 设置摄像头参数
        /// </summary>
        /// <param name="hCamera">摄像头句柄</param>
        /// <param name="nChannel">通道号</param>
        /// <param name="nStream">码流</param>
        /// <param name="pBuffer">参数</param>
        /// <param name="nBufferSize">参数缓冲长度</param>
        /// <param name="nDataLen">参数实际长度</param>
        /// <param name="nMajorCommand">主命令</param>
        /// <param name="nMinorCommand">子命令</param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_SetConfig(IntPtr hCamera, byte nChannel, byte nStream, IntPtr pBuffer, int nBufferSize, int nDataLen, int nMajorCommand, int nMinorCommand);

        /// <summary>
        /// 获取摄像头参数
        /// </summary>
        /// <param name="hCamera"></param>
        /// <param name="nChannel"></param>
        /// <param name="nStream"></param>
        /// <param name="pBuffer"></param>
        /// <param name="nBufferSize"></param>
        /// <param name="nDataLen"></param>
        /// <param name="nMajorCommand"></param>
        /// <param name="nMinorCommand"></param>
        /// <returns></returns>
        [DllImport("KsCamera_AipStar.dll")]
        public static extern Boolean KsCamera_AipStar_GetConfig(IntPtr hCamera, byte nChannel, byte nStream, IntPtr pBuffer, int nBufferSize, int nDataLen, int nMajorCommand, int nMinorCommand);
    }
}
